﻿using ELab.Client;
using ELab.Communication;
using ELab.Communication.Events;
using ELab.Communication.Messages;
using ELab.Management;
using ELab.Message;
using ELab.Organization;
using ELab.Serialization.Data;
using ELab.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ELab.Teacher
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            //-----------------------------
            // Virtual server
            #region Virtual server
            //var _mdsServer = new ELabServer();
            //_mdsServer.Start();
            //_mdsServer.OnMessageReceived += _mdsServer_MessageReceived;
            //var serverInfor = ELabSettings.Instance.Servers.FirstOrDefault();
            //string ipAddressServer = serverInfor == null ? CommunicationConsts.DefaultIpAddress : serverInfor.IpAddress;
            //int portServer = serverInfor == null ? CommunicationConsts.DefaultPort : serverInfor.Port;
            //var controller = new ELab.Management.ELabController(ipAddressServer, portServer);
            //controller.Connect();
            #endregion

            //---------------------
            bool createdNew;
            var m_Mutex = new Mutex(true, "MyProgram", out createdNew);
            if (createdNew)
                Application.Run(new frmTeacherScreen() /*ApplicationListForm(controller)*/);
            else
                MessageBox.Show("The application is already running.", Application.ProductName,
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }


        private static void _mdsServer_MessageReceived(object sender, MessageReceivedFromRemoteApplicationEventArgs e)
        {
            if (e.Message.MessageTypeId == ELabMessageFactory.MessageTypeIdELabDataTransferMessage)
            {
                var eMsg = (ELabDataTransferMessage)e.Message;
                EMessage mymsg = new EMessage(eMsg.MessageData);
                if (mymsg.Command != null)
                {
                    UserProvider provider = new UserProvider();
                    switch (mymsg.Command.CommandType)
                    {
                        case CommandType.RequestAddUser:
                            var userAdd = mymsg.Command.Body.XmlDeserialize<User>();
                            if (!provider.Exist(userAdd))
                            {
                                provider.Add(userAdd);
                            }
                            break;

                        case CommandType.RequestRemoveUser:
                            var userRemove = mymsg.Command.Body.XmlDeserialize<User>();
                            provider.Remove(userRemove);
                            break;

                        case CommandType.RequestLogin:
                            break;

                        case CommandType.RequestLogOut:
                            break;
                    }
                }
            }
        }
    }
}
